package com.dragon.mmochat.model.shop;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.enumType.MmochatPackageGridType;
import com.dragon.mmochat.model.enumType.MmochatPlayerShopState;
import com.dragon.mmochat.model.enumType.MmochatPlayerShopType;
import com.dragon.mmochat.model.object.MmochatPackageGrid;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class MmochatPlayerShop implements Comparable<MmochatPlayerShop> {
	private int id; // 惟一id
	private int roleId; // 店主roleId
	private String roleName; // 店主名字
	private MmochatPlayerShopType shopType; // 类型
	private String name; // 店名
	private long score = 0; // 积分
	private int pageNum = 1; // 柜台页面数
	// 商店格子
	private Map<Integer, MmochatPackageGrid> grids = new ConcurrentHashMap<Integer, MmochatPackageGrid>();
	private int version = 0; // 商店格子的数据发生变化后，version应该提升
	private MmochatPlayerShopState state; // 状态
	private long shopMoney = 0; // 资金
	private String intro = null; // 店主介绍
	private ConcurrentLinkedQueue<String> sellLog = new ConcurrentLinkedQueue<String>();// 销售日志
	private int rank = 0; // 排名
	private int sell_price = 0; // 商店出售价格

	// 数据库回导会调用此构造函数
	public MmochatPlayerShop() {
	}

	// 初始化商店格子
	public void initGrids() {
		for (int i = 0; i < pageNum; i++) {
			for (int j = 0; j < MmochatConstant.gridNumPerShopPage; j++) {
				MmochatPackageGrid grid = new MmochatPackageGrid();
				grid.setCanUse(true);
				grid.setGridIndex(i * MmochatConstant.gridNumPerShopPage + j);
				grid.setGridType(MmochatPackageGridType.玩家商店格子);
				grids.put(grid.getGridIndex(), grid);
			}
		}
		for (int i = pageNum; i < MmochatConstant.maxPageNumPerShop; i++) {
			for (int j = 0; j < MmochatConstant.gridNumPerShopPage; j++) {
				MmochatPackageGrid grid = new MmochatPackageGrid();
				grid.setCanUse(false);
				grid.setGridIndex(i * MmochatConstant.gridNumPerShopPage + j);
				grid.setGridType(MmochatPackageGridType.玩家商店格子);
				grids.put(grid.getGridIndex(), grid);
			}
		}
	}

	public List<MmochatPackageGrid> getGridsByPage(int page) {
		int curPage = Math.min(page, pageNum - 1);
		curPage = Math.max(0, curPage);
		List<MmochatPackageGrid> gridList = new ArrayList<MmochatPackageGrid>();
		for (int j = 0; j < MmochatConstant.gridNumPerShopPage; j++) {
			int gridId = curPage * MmochatConstant.gridNumPerShopPage + j;
			MmochatPackageGrid grid = grids.get(gridId);
			if (grid != null && grid.isCanUse()) {
				gridList.add(grid);
			}
		}
		return gridList;
	}

	public MmochatPackageGrid getFirstSpaceGrid() {
		for (int i = 0; i < pageNum; i++) {
			for (int j = 0; j < MmochatConstant.gridNumPerShopPage; j++) {
				int gridId = i * MmochatConstant.gridNumPerShopPage + j;
				MmochatPackageGrid grid = grids.get(gridId);
				if (grid != null && !grid.hasObj()) {
					return grid;
				}
			}
		}
		return null;
	}

	// 获取格子所在的页面
	public int getPageByGrid(MmochatPackageGrid grid) {
		for (int i = 0; i < pageNum; i++) {
			for (int j = 0; j < MmochatConstant.gridNumPerShopPage; j++) {
				int gridId = i * MmochatConstant.gridNumPerShopPage + j;
				if (grid.getGridIndex() == gridId) {
					return i;
				}
			}
		}
		return 0;
	}

	// 增加一页柜台
	// 增加柜台只允许加到最后页
	public boolean addLastPage() {
		if (pageNum >= MmochatConstant.maxPageNumPerShop) {
			return false;
		}
		synchronized (this) {
			if (pageNum >= MmochatConstant.maxPageNumPerShop) {
				return false;
			}
			pageNum++;
		}
		int base = (pageNum - 1) * MmochatConstant.gridNumPerShopPage;
		for (int j = 0; j < MmochatConstant.gridNumPerShopPage; j++) {
			MmochatPackageGrid grid = grids.get(base + j);
			grid.setCanUse(true);
		}
		return true;
	}

	// 删除最后页柜台
	// 删除柜台只允许删除最后页
	public boolean delLastPage() {
		if (pageNum <= 1) {
			return false;
		}
		int base = (pageNum - 1) * MmochatConstant.gridNumPerShopPage;
		for (int j = 0; j < MmochatConstant.gridNumPerShopPage; j++) {
			MmochatPackageGrid grid = grids.get(base + j);
			grid.setCanUse(false);
		}
		pageNum--;
		pageNum = Math.max(0, pageNum);
		return true;
	}

	// 判断当前柜台是否清空
	public boolean isPageClear(int pageIndex) {
		if (pageIndex < 0 || pageIndex >= pageNum) {
			return true;
		}
		int base = pageIndex * MmochatConstant.gridNumPerShopPage;
		for (int j = 0; j < MmochatConstant.gridNumPerShopPage; j++) {
			MmochatPackageGrid grid = grids.get(base + j);
			if (grid.hasObj()) {
				return false;
			}
		}
		return true;
	}

	// 判断商店所有柜台是否清空
	public boolean isAllPageClear() {
		for (int pageIndex = 0; pageIndex < MmochatConstant.maxPageNumPerShop; pageIndex++) {
			int base = pageIndex * MmochatConstant.gridNumPerShopPage;
			for (int j = 0; j < MmochatConstant.gridNumPerShopPage; j++) {
				MmochatPackageGrid grid = grids.get(base + j);
				if (!grid.isCanUse()) {
					break;
				}
				if (grid.hasObj()) {
					return false;
				}
			}
		}
		return true;
	}

	// 店主身份验证
	public boolean isShopOwner(int myRoleId) {
		return roleId == myRoleId;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public MmochatPlayerShopType getShopType() {
		return shopType;
	}

	public void setShopType(MmochatPlayerShopType shopType) {
		this.shopType = shopType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getScore() {
		return score;
	}

	public void setScore(long score) {
		this.score = score;
	}

	public void addScore(int add) {
		score += add;
		score = Math.max(0, score);
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public Map<Integer, MmochatPackageGrid> getGrids() {
		return grids;
	}

	public void setGrids(Map<Integer, MmochatPackageGrid> grids) {
		this.grids = new ConcurrentHashMap<Integer, MmochatPackageGrid>(grids);
	}

	public MmochatPlayerShopState getState() {
		return state;
	}

	public void setState(MmochatPlayerShopState state) {
		this.state = state;
	}

	public long getShopMoney() {
		return shopMoney;
	}

	public void setShopMoney(long shopMoney) {
		this.shopMoney = shopMoney;
	}

	public void addShopMoney(int add) {
		shopMoney += add;
		shopMoney = Math.max(0, shopMoney);
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public ConcurrentLinkedQueue<String> getSellLog() {
		return sellLog;
	}

	public void setSellLog(ConcurrentLinkedQueue<String> sellLog) {
		this.sellLog = new ConcurrentLinkedQueue<String>(sellLog);
	}

	public void addSellLog(String log) {
		if (sellLog.size() >= MmochatConstant.maxLogNumOfShop) {
			sellLog.poll();
		}
		sellLog.add(log);
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public int getRank() {
		return rank;
	}

	public void setRank(int rank) {
		this.rank = rank;
	}

	public String getGson_grids() {
		if (grids == null) {
			return null;
		}
		Gson gson = new Gson();
		return gson.toJson(grids,
				new TypeToken<Map<Integer, MmochatPackageGrid>>() {
				}.getType());
	}

	public String getGson_sellLog() {
		if (sellLog == null) {
			return null;
		}
		List<String> logList = new ArrayList<String>(sellLog);

		Gson gson = new Gson();
		return gson.toJson(logList, new TypeToken<List<String>>() {
		}.getType());
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public void addVersion() {
		version++;
		if (version >= Integer.MAX_VALUE) {
			version = 0;
		}
	}

	public int getSell_price() {
		return sell_price;
	}

	public void setSell_price(Integer sellPrice) {
		if (sellPrice != null) {
			sell_price = sellPrice;
		}
	}

	@Override
	public int compareTo(MmochatPlayerShop arg0) {
		if (score > arg0.getScore()) {
			return 0;
		} else {
			return 1;
		}
	}

}
